package com.QK.cnstudy.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.QK.cnstudy.entity.Sentence;
import com.QK.cnstudy.entity.WrongSubject;

public class Subject implements Serializable {
    /**
     * 
     */

    private int id;
    private static final long serialVersionUID = 5697081331585531441L;

    /**
     * 问题句子的id号
     */
    private int sentenceId;

    /**
     * 四个选择的句子的id号
     */
    private List<Integer> answerIds;

    /**
     * 正确的答案的序号
     */
    private int answer;

    /**
     * 测试者选择的答案的序号
     */
    private int selectAnswer;

    /**
     * 在一套测试此题的序号
     */
    public int index;

    public Subject() {
        this.id = -1;
    }

    public Subject(WrongSubject wrongSubject) {
        this.id = wrongSubject.getId();
        this.sentenceId = wrongSubject.getSentenceId();

        this.answerIds = new ArrayList<Integer>();
        String a = wrongSubject.getAnswerIds();
        String strings[] = a.substring(1, a.length() - 1).split(",");
        for (String string : strings) {
            this.answerIds.add(Integer.valueOf(string.trim()));
        }

        this.answer = wrongSubject.getAnswer();
    }

    /**
     * 根据问题句子的id号，随机创建出一道题目来，题目的四个选择的句子的id与问题句子的id号不同
     * 
     * @param sentences
     * @param sentenceId
     */
    public Subject(List<Sentence> sentences, int sentenceId) {
        answerIds = new ArrayList<Integer>();
        this.sentenceId = sentenceId;
        answer = ((int) (Math.random() * 4)) % 4;
        List<Integer> otherAnswerIntegers = getOtherAnswerIntegers(sentences, sentenceId);
        int index = 0;
        for (int i = 0; i < 4; i++) {
            if (answer != i) {
                answerIds.add(otherAnswerIntegers.get(index++));
            } else {
                answerIds.add(sentenceId);
            }
        }
        this.id = -1;
    }

    /**
     * 保证 sentencelist个数大于等于4
     * 
     * @param sentencelist
     * @param sentenceId
     * @return
     */
    public List<Integer> getOtherAnswerIntegers(List<Sentence> sentences, int sentenceId) {
        List<Integer> integers = new ArrayList<Integer>();
        int times = 0;

        if (sentences.size() < 4)
            return integers;
        int len = sentences.size();
        while (times < 3) {
            int random = ((int) (Math.random() * len)) % len;
            int randomSentenceId = sentences.get(random).getId();
            if (sentenceId == randomSentenceId)
                continue;
            boolean repeat = false;
            for (int i = 0; i < integers.size(); i++) {
                if (integers.get(i) == randomSentenceId) {
                    repeat = true;
                    break;
                }
            }
            if (repeat == true) {
                continue;
            }
            integers.add(randomSentenceId);
            times++;
        }
        return integers;
    }

    public int getSentenceId() {
        return sentenceId;
    }

    public void setSentenceId(int sentenceId) {
        this.sentenceId = sentenceId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }

    public List<Integer> getAnswerIds() {
        return answerIds;
    }

    public void setAnswerIds(List<Integer> answerIds) {
        this.answerIds = answerIds;
    }

    public int getSelectAnswer() {
        return selectAnswer;
    }

    public void setSelectAnswer(int selectAnswer) {
        this.selectAnswer = selectAnswer;
    }

}
